package com.centaline.androidsalesblog.overlay.local;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.AppCompatTextView;
import android.view.LayoutInflater;
import android.view.View;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.base.BaseOverlay;
import com.centaline.androidsalesblog.constants.ExtraConstant;
import com.centaline.androidsalesblog.iml.BaiduMarkerClick;
import com.centaline.androidsalesblog.model.databaseobject.EsfEstateDo;

import java.util.ArrayList;
import java.util.Locale;

/**
 * 地图找房租售列表
 * Created by guilin on 16/5/11.
 */
public class EsfMainMapOverlay extends BaseOverlay<EsfEstateDo> {

    final String postType;

    private View view;
    private AppCompatTextView atv_mark_title;

    private View detail, clickView;
    private AppCompatTextView atv_detail_num, atv_detail_title, atv_select_num, atv_select_title;
    private Overlay selectOverlay;

    private ArrayList<Overlay> detaliOverlayList;

    public EsfMainMapOverlay(Context context, BaiduMap baiduMap, String postType, ArrayList<EsfEstateDo> data,
                             BaiduMarkerClick baiduMarkerClick) {
        super(context, baiduMap, data, baiduMarkerClick);
        this.postType = postType;
        detaliOverlayList = new ArrayList<>();
    }

    @Override
    protected void initView() {
        view = LayoutInflater.from(context).inflate(R.layout.layout_map_new_est, null);
        atv_mark_title = (AppCompatTextView) view.findViewById(R.id.atv_mark_title);

        detail = LayoutInflater.from(context).inflate(R.layout.layout_map_esf, null);
        atv_detail_num = (AppCompatTextView) detail.findViewById(R.id.atv_mark_num);
        atv_detail_title = (AppCompatTextView) detail.findViewById(R.id.atv_mark_title);

        clickView = LayoutInflater.from(context).inflate(R.layout.layout_map_esf_xz, null);
        atv_select_num = (AppCompatTextView) clickView.findViewById(R.id.atv_mark_num);
        atv_select_title = (AppCompatTextView) clickView.findViewById(R.id.atv_mark_title);
    }

    /**
     * 更新地图,清除旧数据
     */
    public void mapStatusChangeAndClear() {
        if (checkAll())
            return;
        if (selectOverlay != null) {
            selectOverlay.remove();
        }
        for (Overlay overlay : detaliOverlayList) {
            overlay.remove();
        }
        detaliOverlayList.clear();
        for (Overlay overlay : overlayList) {
            overlay.remove();
        }
        overlayList.clear();
        mapStatusChange();
    }

    @Override
    public void mapStatusChange() {
        if (checkAll())
            return;
        float zoom = baiduMap.getMapStatus().zoom;
        if (selectOverlay != null) {
            selectOverlay.remove();
        }
        ArrayList<Overlay> tempOverlay = new ArrayList<>();
        ArrayList<String> ids = new ArrayList<>();
        if (zoom < 17.1) {
            //小区层级1级
            for (Overlay overlay : detaliOverlayList) {
                overlay.remove();
            }
            detaliOverlayList.clear();
            for (Overlay overlay : overlayList) {
                Marker marker = (Marker) overlay;
                EsfEstateDo esfEstateDo = marker.getExtraInfo().getParcelable(ExtraConstant.MAP_EXTRA_INFO);
                if (esfEstateDo != null &&
                        baiduMap.getMapStatusLimit().contains(marker.getPosition())) {
                    ids.add(esfEstateDo.getCestCode());
                    tempOverlay.add(overlay);
                } else {
                    overlay.remove();
                }
            }
            overlayList.clear();
            overlayList.addAll(tempOverlay);
            int size = data.size();
            for (int i = 0; i < size; i++) {
                if (!ids.contains(data.get(i).getCestCode()) &&
                        isInBound(data.get(i))) {
                    overlayList.add(baiduMap.addOverlay(getMarkerOptions(data.get(i))));
                }
            }
        } else {
            //小区层级2级
            for (Overlay overlay : overlayList) {
                overlay.remove();
            }
            overlayList.clear();
            for (Overlay overlay : detaliOverlayList) {
                Marker marker = (Marker) overlay;
                EsfEstateDo esfEstateDo = marker.getExtraInfo().getParcelable(ExtraConstant.MAP_EXTRA_INFO);
                if (esfEstateDo != null &&
                        baiduMap.getMapStatusLimit().contains(marker.getPosition())) {
                    ids.add(esfEstateDo.getCestCode());
                    tempOverlay.add(overlay);
                } else {
                    overlay.remove();
                }
            }
            detaliOverlayList.clear();
            detaliOverlayList.addAll(tempOverlay);
            int size = data.size();
            for (int i = 0; i < size; i++) {
                if (!ids.contains(data.get(i).getCestCode()) &&
                        isInBound(data.get(i))) {
                    detaliOverlayList.add(baiduMap.addOverlay(getMarkerOptionsForDetail(data.get(i))));
                }
            }
        }
    }

    @Override
    public void removeFromMap() {
        super.removeFromMap();
        for (Overlay overlay : detaliOverlayList) {
            overlay.remove();
        }
        detaliOverlayList.clear();
        if (selectOverlay != null) {
            selectOverlay.remove();
        }
    }

    @Override
    protected OverlayOptions getMarkerOptions(EsfEstateDo esfEstateDo) {
        Bundle bundle = new Bundle();
        bundle.putParcelable(ExtraConstant.MAP_EXTRA_INFO, esfEstateDo);
        StringBuilder price = new StringBuilder();
        if ("s".equalsIgnoreCase(postType)) {
            price.append(esfEstateDo.getCestAvgPrice() > 10000 ?
                    String.format(Locale.CHINA, "%.1f万", esfEstateDo.getCestAvgPrice() / 10000) :
                    String.format(Locale.CHINA, "%.0f元", esfEstateDo.getCestAvgPrice()));
        } else {
            price.append(String.format(Locale.CHINA, "%d套", esfEstateDo.getRentCount()));
        }
        atv_mark_title.setText(price.toString());
        return new MarkerOptions()
                .position(new LatLng(esfEstateDo.getLat(), esfEstateDo.getLng()))
                .extraInfo(bundle)
                .icon(BitmapDescriptorFactory.fromView(view));
    }

    private OverlayOptions getMarkerOptionsForDetail(EsfEstateDo esfEstateDo) {
        Bundle bundle = new Bundle();
        bundle.putParcelable(ExtraConstant.MAP_EXTRA_INFO, esfEstateDo);
        StringBuilder price = new StringBuilder();
        if ("s".equalsIgnoreCase(postType)) {
            price.append(esfEstateDo.getCestAvgPrice() > 10000 ?
                    String.format(Locale.CHINA, "%.1f万", esfEstateDo.getCestAvgPrice() / 10000) :
                    String.format(Locale.CHINA, "%.0f元", esfEstateDo.getCestAvgPrice()));
        } else {
            price.append(String.format(Locale.CHINA, "%d套", esfEstateDo.getRentCount()));
        }
        atv_detail_num.setText(price.toString());
        atv_detail_title.setText("s".equalsIgnoreCase(postType) ?
                String.format(Locale.CHINA, "%s %d套",
                        esfEstateDo.getName(), esfEstateDo.getSaleCount()) :
                esfEstateDo.getName());
        return new MarkerOptions()
                .position(new LatLng(esfEstateDo.getLat(), esfEstateDo.getLng()))
                .extraInfo(bundle)
                .anchor(0.2f, 1.0f)
                .icon(BitmapDescriptorFactory.fromView(detail));
    }

    /**
     * 移除
     */
    public void removeSelect() {
        if (selectOverlay != null) {
            selectOverlay.remove();
        }
    }

    @Override
    public boolean onMarkerClick(Marker marker) {
        if (overlayList.contains(marker)) {
            baiduMap.animateMapStatus(MapStatusUpdateFactory.newLatLngZoom(marker.getPosition(), 18f));
        } else if (detaliOverlayList.contains(marker)) {
            if (selectOverlay != null) {
                selectOverlay.remove();
            }
            EsfEstateDo esfEstateDo = marker.getExtraInfo().getParcelable(ExtraConstant.MAP_EXTRA_INFO);
            if (esfEstateDo != null) {
                StringBuilder price = new StringBuilder();
                if ("s".equalsIgnoreCase(postType)) {
                    price.append(esfEstateDo.getCestAvgPrice() > 10000 ?
                            String.format(Locale.CHINA, "%.1f万", esfEstateDo.getCestAvgPrice() / 10000) :
                            String.format(Locale.CHINA, "%.0f元", esfEstateDo.getCestAvgPrice()));
                } else {
                    price.append(String.format(Locale.CHINA, "%d套", esfEstateDo.getRentCount()));
                }
                atv_select_num.setText(price.toString());
                atv_select_title.setText("s".equalsIgnoreCase(postType) ?
                        String.format(Locale.CHINA, "%s %d套",
                                esfEstateDo.getName(), esfEstateDo.getSaleCount()) :
                        esfEstateDo.getName());
                selectOverlay = baiduMap.addOverlay(new MarkerOptions()
                        .position(new LatLng(esfEstateDo.getLat(), esfEstateDo.getLng()))
                        .extraInfo(marker.getExtraInfo())
                        .anchor(0.2f, 1.0f)
                        .icon(BitmapDescriptorFactory.fromView(clickView)));
            }
            return baiduMarkerClick.onMarkerClick(marker);
        }
        return false;
    }

    @Override
    protected boolean isInBound(EsfEstateDo esfEstateDo) {
        return baiduMap.getMapStatusLimit().contains(new LatLng(esfEstateDo.getLat(), esfEstateDo.getLng()));
    }
}
